<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection" :searchInfo="searchInfo">
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts" setup name="orderComment">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { Api } from './data/api';
import { columns, searchFormSchema } from './data/data';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { reactive } from 'vue';

const { tableContext, handlePost } = useBaseTable({
  tableProps: {
    title: '订单评论列表',
    listUrl: Api.page,
    handlerName: 'busOrderCommentListHandler',
    delUrl: Api.remove,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      showExportButton: true,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: true,
    clickToRowSelect: true,
    rowKey: 'orderCommentId',
    actionColumn: {
      width: 150,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    rowSelection: {
      type: 'radio',
    },
  },
});

//注册table数据
const [registerTable, { reload }, { rowSelection, selectedRow, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '上架',
      popConfirm: {
        title: '确认上架评论吗?',
        confirm: handleSetState.bind(null, record),
      },
      ifShow: _action => {
        return record.state == 0;
      },
    },
    {
      label: '下架',
      popConfirm: {
        title: '确认下架评论吗?',
        confirm: handleSetState.bind(null, record),
      },
      ifShow: _action => {
        return record.state == 1;
      },
    },
  ];
}

/**
 * 更新发布状态
 */
async function handleSetState(record) {
  await handlePost(Api.setState, {
    ids: record.orderCommentId,
    state: record.state == 1 ? 0 : 1,
  });
}

const searchInfo = reactive<Recordable>({});

function setSearchInfoProps(orderId) {
  searchInfo.orderId = orderId;
}

defineExpose({
  setSearchInfoProps,
});
</script>
